package com.ps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ps.mapper.PremissionMapper;
import com.ps.mapper.RolePremissionMapper;
import com.ps.pojo.Premission;
import com.ps.pojo.Role;
import com.ps.mapper.RoleMapper;
import com.ps.pojo.RolePremission;
import com.ps.service.PremissionService;
import com.ps.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ps.vo.TreeNode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.Permission;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-12-18
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PremissionService premissionService;
    @Resource
    private RolePremissionMapper rolePremissionMapper;
    @Resource
    private PremissionMapper premissionMapper;
    @Override
    public boolean add(Role role) {
        return roleMapper.insert(role)>0;
    }
    private List<TreeNode>getParentId(Integer parentId,Integer roleid){
        LambdaQueryWrapper<Premission> wrapper=new LambdaQueryWrapper<>();
        if (parentId!=null){
            wrapper.eq(Premission::getParentid,parentId);
        }else{
            wrapper.isNull(Premission::getParentid);
        }
        List<Premission> list = premissionMapper.selectList(wrapper);
        return list.stream().map(premission -> {
            TreeNode treeNode=new TreeNode();
            treeNode.setId(premission.getId());
            treeNode.setTitle(premission.getFunctionname());
            List<Integer> permid = rolePremissionMapper.getPermid(roleid);
            int count=premissionMapper.selectCount
                    (Wrappers.<Premission>query()
                            .eq("parentid",premission.getId()));
            if (permid.contains(premission.getId())&&count==0){
                treeNode.setChecked(true);
            }
            return treeNode;
        }).collect(Collectors.toList());
    }
    @Override
    public List<TreeNode> getTreeData(Integer roleid) {
        List<TreeNode> list01=getParentId(null,roleid);
        for (TreeNode l01:list01){
            List<TreeNode>list02=getParentId(l01.getId(),roleid);
            l01.setChildren(list02);
            for (TreeNode l02:list02){
                List<TreeNode>list03=getParentId(l02.getId(),roleid);
                l02.setChildren(list03);
            }
        }
        return list01;
    }

    @Override
    public Boolean grant(Integer roleid, Integer[] ids) {
        boolean flag=true;
        rolePremissionMapper.delete(Wrappers.<RolePremission>update()
                .eq("roleid",roleid));
        for (Integer id:ids){
            RolePremission rolePremission=new RolePremission();
            rolePremission.setRoleid(roleid);
            rolePremission.setPermid(id);
            if (rolePremissionMapper.insert(rolePremission)==0){
                flag=false;
            }

        }
        return flag;
    }
}
